﻿using Microsoft.AspNetCore.Mvc.Formatters.Json.Internal;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc.Infrastructure;

namespace RAP.Web.Libary.Controller
{
  public class JsonResult : ContentResult
  {


    public JsonResult(object value)
    {
      this.Value = value;
      Content = JsonConvert.SerializeObject(value);
      ContentType = "application/json";
    }

    //
    // 摘要:
    //     Creates a new Microsoft.AspNetCore.Mvc.JsonResult with the given value.
    //
    // 参数:
    //   value:
    //     The value to format as JSON.
    //
    //   serializerSettings:
    //     The Newtonsoft.Json.JsonSerializerSettings to be used by the formatter.
    public JsonResult(object value, JsonSerializerSettings serializerSettings)
    {
      if (serializerSettings == null)
      {
        throw new ArgumentNullException("serializerSettings");
      }

      this.Value = value;
      Content = JsonConvert.SerializeObject(value, serializerSettings);
      ContentType = "application/json";
    }

    public override Task ExecuteResultAsync(ActionContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      return context.HttpContext.RequestServices.GetRequiredService<ContentResultExecutor>().ExecuteAsync(context, this);
    }

    public Object Value
    {
      get;set;
    }
  }
}
